package com.cy.java.stream;

import java.sql.SQLOutput;
import java.util.ArrayList;
/*遍历姓张且长度为3的元素*/
public class streamdemo {
    public static void main(String[] args) {
        //创建一个集合，存储多个字符串元素
        ArrayList<String> list=new ArrayList<String>();
        list.add("张三");
        list.add("张四");
        list.add("张三四");
        list.add("李四");
        list.add("李三四");
        list.add("李三");
    list.stream().limit(2).forEach(s -> System.out.println(s));
    list.stream().skip(2).forEach(System.out::println);
       /* //将集合所有性张的元素存储到新的集合
        ArrayList<String> newlist=new ArrayList<String>();
        for (String s:list){
            if (s.startsWith("张")){
                newlist.add(s);
            }
        }
        System.out.println(newlist);


        //将姓张且长度为3的元素存储到新的元素
        ArrayList<String> threelist=new ArrayList<String>();
        for (String s:newlist){
            if (s.length()==3){
                threelist.add(s);
            }
        }
        System.out.println(threelist);
    //遍历姓张且长度为3的元素
    for (String s:threelist){
        System.out.println(s);
    }

    //stream
list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()==3).forEach(s -> System.out.println(s));
    list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()==3).forEach(s -> System.out.println(s));*/

    }

}
